package com.gerfmarquez.nymph.ui.listeners;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gerfmarquez.nymph.ui.numberpicker.NumberPicker;
import com.gerfmarquez.nymph.ui.seekbar.SpawnNoteView;
import com.gerfmarquez.nymph.ui.seekbar.VerticalSeekBar;
import com.gerfmarquez.nymph.ui.seekbar.ViewInflater;

public class DialogListenerActionMusicNote implements DialogInterface.OnClickListener {
	Context context;
	VerticalSeekBar vsb;
	private final static String TAG = "DialogListenerActionMusicNote";
	final DialogListenerActionMusicNote dlamn;
	int numberOfNotes;
	RadioButton rbLeft;
	RadioButton rbRight;
	
	public DialogListenerActionMusicNote(Context context) {
		this.context = context;
		dlamn = this;
	}
	public void onClick(DialogInterface dialog, final int item) {
		final LinearLayout individualNote = (LinearLayout)vsb.getParent();
		final LinearLayout horizontalMusicNotesLayout = (LinearLayout)individualNote.getParent();
		final int indexChild = horizontalMusicNotesLayout.indexOfChild(individualNote);
//        Toast.makeText(context, items[item], Toast.LENGTH_SHORT).show();
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		final LinearLayout viewGroup = new LinearLayout(context);
		final LinearLayout numPickLay = ViewInflater.getNumberPicker(context);
		final RelativeLayout relLay = new RelativeLayout(context);
		RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
		rParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
		relLay.setLayoutParams(rParams);

		viewGroup.setLayoutParams(rParams);
		viewGroup.setOrientation(0);
		rbLeft = new RadioButton(context);
		rbLeft.setText("To Left");
		rbRight = new RadioButton(context);
		rbRight.setText("To Right");
		final RadioGroup rg = new RadioGroup(context);
		final OnCheckedChange change = new OnCheckedChange();
		rg.setOnCheckedChangeListener(change);
		rg.addView(rbLeft);
		rg.addView(rbRight);
		
		viewGroup.addView(rg);
		viewGroup.addView(numPickLay);
		relLay.addView(viewGroup);
		
		builder.setView(relLay);
		
		builder.setTitle("How many music notes to add or remove?");
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
//				NumberPicker np = (NumberPicker)numPickLay.getChildAt(0);
				TextView tv = (TextView)numPickLay.getChildAt(1);
				int numberOfNotes = Integer.parseInt(tv.getText().toString());
				dlamn.numberOfNotes = numberOfNotes;
//				Log.d(TAG,"choice = "+ change.choice);
//				Log.d(TAG,"rbLeft = "+ rbLeft.getId());
//				Log.d(TAG,"rbRight = "+ rbRight.getId());
				dlamn.doWorkAddOrRemoveNotes(individualNote, indexChild,  builder, item, horizontalMusicNotesLayout, change.choice);
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

    }
	public void setVerticalSeekBar(VerticalSeekBar vsb) {
		this.vsb = vsb;
	}
	private void doWorkAddOrRemoveNotes(LinearLayout individualNote,int indexChild,AlertDialog.Builder builder,
			int item, LinearLayout horizontalMusicNotesLayout, int side) {
		for(int i = 0; i < dlamn.numberOfNotes; i++) {
			Log.d(TAG,"loop: "+i);
			switch(item) {
			case 0://Add
	//			Add Music note view
				if(side == rbRight.getId())
					indexChild++;
				if(side == -1)
					indexChild++;
				LinearLayout musicNoteLinearVerticalLayout = SpawnNoteView.spawnNoteView(context);
				LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
				((LinearLayout)individualNote.getParent()).addView(musicNoteLinearVerticalLayout, indexChild, layoutParams);
				Log.d("OnTouchListenerSeekBar", "Added view in index of parent = "+indexChild);
				break;
			case 1://Remove
	//			Remove music note view
//				horizontalMainLayout.removeView(individualNote);
				if(side == rbLeft.getId())
					indexChild--;
				else if(side == rbRight.getId())
					indexChild++;
				LinearLayout individualNoteUpdated = (LinearLayout)horizontalMusicNotesLayout.getChildAt(indexChild);
				horizontalMusicNotesLayout.removeView(individualNoteUpdated);
				individualNoteUpdated.setVisibility(View.GONE);
				Log.d("OnTouchListenerSeekBar", "Removed view with id = "+indexChild);
				break;
			case 2://other
				break;
			default:
				break;
			}
		}
	}
}
